<?php

/**
 * @Author: wuchenge
 * @Date:   2022-04-26 14:51:22
 * @Email: ligui0506@126.com
 * @Descript:
 */
namespace App\Admin\Controllers;

use App\Events\UserRecord as UserRecordEvent;
use App\Http\Controllers\ApiController;
use App\Models\User;
use App\Models\UserInfo;
use App\Models\UserRecord;
use App\Transformer\UserTransformer;
use Illuminate\Http\Request;

class UserController extends ApiController
{

    /**
     * @var mixed
     */
    protected $request;

    /**
     * @var mixed
     */
    protected $user_transformer;

    /**
     * @param Request $request
     */
    public function __construct(Request $request, UserTransformer
         $user_transformer) {
        $this->request          = $request;
        $this->user_transformer = $user_transformer;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function changeLevel(Request $request)
    {
        $id    = $request->input('id');
        $level = $request->input('level');

        $res = UserInfo::where('user_id', $id)->update([
            'levels' => $level,
        ]);

        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function changeStatus(Request $request)
    {
        $id     = $request->input('id');
        $status = $request->input('status');

        $res = User::where('id', $id)->update([
            'status' => $status,
        ]);

        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    /**
     * @param Request $request
     */
    public function addRecord(Request $request)
    {
        $id       = $request->input('id');
        $money    = $request->input('money');
        $admin_id = $request->get('admin')->id;

        if (empty(intval($money))) {
            return $this->failed('消费必须大于0');
        }

        $res = UserRecord::store($id, $money, $admin_id);
        if ($res) {
            // 发送事件
            event(new UserRecordEvent(['user_id' => $id, 'money' => $money]));
            return $this->response();
        }

        return $this->failed();
    }

    /**
     * @param ArticleRequest $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $keyword = $request->input('keyword', '');
        $filter  = $request->input('filter', '');
        $sort    = $request->input('sort', '');
        $page    = $request->input('page', 1);
        $limit   = $request->input('limit', 10);

        $where = ['nomal' => []];
        if (!empty($keyword)) {
            $like = ['email', 'like', $keyword . '%'];
            array_push($where['nomal'], $like);
        }

        if (!empty($filter)) {
            $kv = explode('-', $filter);
            array_push($where['nomal'], $kv);
        }

        $order = [];
        if (!empty($sort)) {
            $kv            = explode('-', $sort);
            $order[$kv[0]] = $kv[1];
        }

        $field = ['*'];
        $model = User::with(['info', 'info.level'])
            ->withCount(['children']);

        $data = User::lists($model, $field, $where, $page, $limit,
            $order);
        foreach ($data['data'] as $key => $value) {
            $data['data'][$key] = $this->user_transformer->transform($value);
        }
        return $this->response($data);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function recordList(Request $request, $id)
    {
        $keyword = $request->input('keyword', '');
        $filter  = $request->input('filter', '');
        $sort    = $request->input('sort', '');
        $page    = $request->input('page', 1);
        $limit   = $request->input('limit', 10);

        $where = ['nomal' => [
            ['user_id', $id],
        ]];
        if (!empty($keyword)) {
            $like = ['money', $keyword];
            array_push($where['nomal'], $like);
        }

        if (!empty($filter)) {
            $kv = explode('-', $filter);
            array_push($where['nomal'], $kv);
        }

        $order = [];
        if (!empty($sort)) {
            $kv            = explode('-', $sort);
            $order[$kv[0]] = $kv[1];
        }

        $field = ['*'];
        $model = UserRecord::with(['admin', 'user', 'user.info']);

        $data = UserRecord::lists($model, $field, $where, $page, $limit,
            $order);
        return $this->response($data);
    }

}
